package com.dayu.demo.dao;

import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import com.vecspace.data.model.FilterColumn;
import com.vecspace.data.model.Pagination;
import com.vecspace.database.dao.AbstractDao;
import com.vecspace.database.dao.DaoUtil;
import com.vecspace.database.model.QueryResultAggregation;
import com.vecspace.database.model.QuerySQL;
import com.vecspace.utils.Assert;
import com.dayu.demo.pojo.Department;
import com.dayu.demo.pojo.DepartmentEx;
import com.dayu.demo.pojo.DepartmentStats;

/** 部门,操作数据库的dao类
 * @template dayu-code-builder
 * @author user
 * @reviewer 
 * @createTime 2021-11-24 15:11:41
 * @version 1.0
 */
@Repository("demo_departmentDao")
public class DepartmentDao extends AbstractDao {
    /** 根据ID查询 
     * @param id   主键值
     * @return Department
     * @throws Exception
     * @author user
     * @reviewer 
     * @createTime 2021-11-24 15:11:41
     */
    public Department getDepartment(Long id) throws Exception {
        Department department = null;
        if (id != null) {
            department = dao.queryObject(Department.class, id);
            this.dao.evict(department);
        }
        return department;
    }

    /**根据父节点id获取子节点
     *
     * @param parentId
     * @return
     * @throws Exception
     */
    public List<Department> getChildDepartmentList(Long parentId) throws Exception {
        List<Department> departmentList = dao.queryObjectList("from Department where parentId=?0 order by ordnum", new Object[]{parentId});
        return departmentList;
    }

    /**根据 上级部门ID 获取部门记录条数
     *
     * @param parentId  上级部门ID
     * @return
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public int getDepartmentCountByParent(Long parentId) throws Exception {
        int rcount = dao.queryInt("select count(*) from T_DEMO_DEPARTMENT where PARENT_ID=?0 order by ordnum", new Object[]{parentId});
        return rcount;
    }


    /**根据属性名获取字段值
     * @param id 记录id
     * @param propName 属性名
     * @return
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public Object getValueByProp(Long id, String propName) throws Exception {
    	return this.getValueByPropName(Department.class, id, propName);
    }

    /**根据id更新指定字段的值（根据propName从pojo获取字段名）
     * @param id 记录id
     * @param propName 属性名
     * @param value 属性值
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public void updateValueByProp(Long id, String propName, Object value) throws Exception {
    	this.updateValueByPropName(Department.class, id, propName, value);
    }

    /** 插入或修改 
     * @param item 要保存的对象
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public void saveDepartment(Department item) throws Exception {
        dao.saveOrUpdateObject(item);
        dao.evict(item);
    }

    /**更改 自己所属的父节点
     *
     * @param id T_DEMO_DEPARTMENT.id
     * @param parentIdNew 新的 上级部门ID
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public void changeParent(Long id, Long parentIdNew) throws Exception {
        String sql = "update T_DEMO_DEPARTMENT set PARENT_ID=? where ID=?";
        dao.updateDataRowBySql(sql, new Object[]{parentIdNew, id});
    }


    /** 根据ID删除 
     * @param id  主键值
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public void deleteDepartment(Long id) throws Exception {
        if (id != null) {
            dao.deleteObject(Department.class, id);
        }
    }
    
    /**根据多个查询条件执行分页查询 （可以分页，不分页，以及同时进行聚合统计查询，,聚合统计查询是对所有满足条件的数据进行聚合，而不仅仅是当前页的数据）
     * 
     
     
     * @param filterColumns 查询条件
     * @param pageIndex 第几页 从1开始
     * @param pageSize 每页显示的数据条数,负数表示不分页
     * @param sortColumn 排序字段（属性名或者字段名）
     * @param sortOrder 排序方式 DESC ASC
     * @return
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    public QueryResultAggregation<DepartmentEx, DepartmentStats> getDepartmentExListAndStats(List<FilterColumn> filterColumns,
        int pageIndex, int pageSize, String sortColumn, String sortOrder) throws Exception {

        QuerySQL querySql = DaoUtil.getQuerySql(this.getClass(), "querysql");
        Assert.throwz(querySql != null && querySql.valid(), "Employee.sql.xml中<sql id='querysql'>没有定义SQL,或者定义的格式不正确");

        if(StringUtils.isNotBlank(sortColumn)) {
            querySql.setOrderby(sortColumn, sortOrder);
        }
        QueryResultAggregation<DepartmentEx, DepartmentStats> departmentList = dao.queryEntitiesAndAggregate(DepartmentEx.class, DepartmentStats.class, querySql, new Pagination(pageIndex, pageSize));

        return departmentList;
    }
}
